package com.xl.web.controller.fns;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xl.core.util.StringUtil;
import com.xl.web.entity.area.Area;
import com.xl.web.entity.dict.DictBase;
import com.xl.web.service.area.AreaService;
import com.xl.web.service.dict.DictService;

/**
 * 自定义标签取值
 * 
 * @author 马新玉
 */
@Component
public class FnsBaseUtil {

	@Autowired(required = false)
	private DictService<DictBase> dictService;
	
	// 注入省市区管理service
	@Autowired(required = false)
	private AreaService<Area> areaService;

	private static FnsBaseUtil dictBaseUtil;

	@PostConstruct
	public void init() {
		dictBaseUtil = this;
		dictBaseUtil.dictService = this.dictService;
	}

	/**
	 * 根据类型type和编码code获取唯一的字典值
	 * 
	 * @param type
	 * @param code
	 * @return
	 */
	public static String getDictLabel(String type, String code) {
		String result = "-";
		DictBase tempDict = null;
		DictBase dictBase = new DictBase();
		dictBase.setDictType(type);
		dictBase.setDictValue(code);
		if (!"".equals(code) && !"".equals(type)) {
			tempDict = dictBaseUtil.dictService.getDictLabel(dictBase);
		}
		if (tempDict != null) {
			result = tempDict.getDictName();
		}
		return result;
	}

	/**
	 * 根据类型type和编码code获取唯一的字典值
	 * 
	 * @param type
	 * @param code
	 * @return
	 */
	public static String getDictValue(String type, String name) {
		String result = "-";
		DictBase tempDict = null;
		DictBase dictBase = new DictBase();
		dictBase.setDictType(type);
		dictBase.setDictName(name);
		if (!"".equals(name) && !"".equals(type)) {
			tempDict = dictBaseUtil.dictService.getDictName(dictBase);
		}
		if (tempDict != null) {
			result = tempDict.getDictValue();
		}
		return result;
	}
	
	/**
	 * 根据类型type和编码code获取多个字典的取值
	 * 
	 * @param type
	 * @param code
	 * @return
	 */
	public static String getDictLabels(String type, String codes) {
		String result = "-";
		String[] arrayStr = codes.split(",");// 字符串转字符数组
		List<String> list = java.util.Arrays.asList(arrayStr);// 字符数组转list
		DictBase dictBase = new DictBase();
		dictBase.setDictType(type);
		dictBase.setDictValues(list);
		List<DictBase> dictBaseList = dictBaseUtil.dictService.getDictLabels(dictBase);
		if(dictBaseList.size()!=0){
			result="";
			for(DictBase i:dictBaseList){
				result+=i.getDictName()+" ";
			}
		}
		return result;
	}
	
	/**
	 * 根据类型type获取字典列表
	 * 
	 * @param type
	 * @param code
	 * @return
	 */
	public static List<DictBase> getDictList(String type) {
		List<DictBase> result = null;
		if (!StringUtil.isEmpty(type)) {
			result = dictBaseUtil.dictService.getDictList(type);
		}
		if (result == null) {
			result = new ArrayList<DictBase>();
		}
		return result;
	}
	
	
	/**
	 * 获取对应地址
	 * @param areaId
	 * @return
	 * @throws Exception
	 */
	public static String getAddress(String areaId) throws Exception {
		String result = "-";
		// 根据给定条件获取到省市区列表
		Area area = dictBaseUtil.areaService.queryById(areaId);
		if(area!=null){
			result=area.getAreaname();
		}
		return result;
	}
}
